# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo
import traceback
from BashouScrapy import settings

def get_mongo_client():
    return pymongo.MongoClient(host=settings.MONGODB_HOST, port=settings.MONGODB_PORT, connect=False, socketKeepAlive=True, wtimeout=10000, w=1, waitQueueTimeoutMS=10000)

def get_wfuu_db():
    client = get_mongo_client()
    db = client["wfuu_db"]
    client["admin"].authenticate(settings.username, settings.password)
    return db

# def get_crawl_db():
#     client = get_mongo_client()
#     db = client["craw_db"]
#     client["admin"].authenticate(settings.username, settings.password)
#     return db

def get_yindan_test():
    client = get_mongo_client()
    db = client["yindan_test"]
    client["admin"].authenticate(settings.username, settings.password)
    return db

class MongoBase(object):
    mongo_client = get_mongo_client()
    # wfuu_db = get_wfuu_db()
    # crawl_db = get_crawl_db()
    yindan_db = get_yindan_test()

class IndictmentListPipeline(object):
    def __init__(self):
        crawl_db = MongoBase.yindan_db
        self.indictment = crawl_db["indictment"]

    def process_item(self, item, spider):
       postItem = dict(item)
       try:
           self.indictment.insert(postItem)
       except pymongo.errors.DuplicateKeyError:
           pass
       except Exception as error:
           print(error)
       # return item

class IndictmentContentPipeline(object):
    def __init__(self):
        crawl_db = MongoBase.yindan_db
        self.indictment = crawl_db["indictment"]

    def process_item(self, item, spider):
        indictment_type = item['type']
        try:
            if indictment_type:
                self.indictment.update_one({'_id': item['_id']}, {'$set': {'type': indictment_type}})
            self.indictment.update_one({'_id': item['_id']}, {'$set': {'rawData': item['rawData'], 'hasDoc': True}})
        except Exception as error:
            print(error)
# 文书网 --列表
class WenshuListPipeline(object):
    def __init__(self):
        crawl_db = MongoBase.yindan_db
        self.collection = crawl_db["indictment"]
    def process_item(self,item,spider):
        try:
           self.collection.insert_one(item)
        except pymongo.errors.DuplicateKeyError:
            pass
        except Exception as error:
            print(error)
# 文书网 --详情
class WenshuContentPipeline(object):
    def __init__(self):
        crawl_db = MongoBase.yindan_db
        self.collection = crawl_db["indictment"]
    def process_item(self, item, spider):
        update_data = {}
        update_data["hasDoc"] = True
        update_data["DocContent"] = item["DocContent"]
        if item["PubDate"] != '':
            update_data['PubDate'] = item["PubDate"]
            self.collection.update_one({"_id": item["_id"]}, {"$set": update_data})
        return item
# 文书网 --summary
class WenshuSummaryPipeline(object):
    def __init__(self):
        crawl_db = MongoBase.yindan_db
        self.collection = crawl_db["indictment"]
    def process_item(self, item, spider):
        _id = item["_id"]
        self.collection.update_one({"_id": _id}, {"$set": {"summary": item["summary"], "hasSummary": True}})